package com.hz.controller;

import com.hz.pojo.Advertising;
import com.hz.service.AdvertisingService;
import com.hz.utils.MassageJson;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Advertising")
public class AdvertisingController {

    @Resource
    private AdvertisingService advertisingService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 根据广告id获取广告内容
     * @return
     */
    @RequestMapping("/getContent")
    public MassageJson getContent(Long advertising_id){
        Advertising advertising = null;
        try {
            advertising = advertisingService.getAdvertisingContent(advertising_id);
            stringRedisTemplate.opsForValue().increment(advertising_id+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(advertising!=null){
            return new MassageJson(200,"获取成功",advertising);
        }else {
            return new MassageJson(201,"获取失败",advertising);
        }

    }

    /**
     * 添加一条新的广告 传入一个广告对象
     * @return
     */
    @RequestMapping("/addAdvertising")
    public MassageJson addAdvertisingContent(Advertising advertising){
        int i = 0;
        try {
            advertising.setAdvertising_state(1);
            advertising.setAdvertising_traffic(0L);
            i = advertisingService.addAdvertisingContent(advertising);
            if(i>0){
                List<Advertising> list = advertisingService.getAll();
                return new MassageJson(200,"添加成功",list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            return new MassageJson(201,"添加失败",null);

    }

    /**
     * 修改 一条广告 传入一个广告对象
     * @return
     */
    @RequestMapping("/updAdvertising")

    public MassageJson updAdvertisingContent(Advertising advertising){
        int i = 0;
        try {
            i = advertisingService.updAdvertisingContent(advertising);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(i>0){
            return new MassageJson(200,"修改成功",advertising);
        }else {
            return new MassageJson(201,"修改失败",advertising);
        }

    }

    /**
     * 删除一条广告 传入一个广告id
     * @return
     */
    @RequestMapping("/delAdvertising")

    public MassageJson delAdvertisingContent(Long advertising_id){
        int i = 0;
        try {
            i = advertisingService.delAdvertisingContent(advertising_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(i>0){
            return new MassageJson(200,"删除成功",i);
        }else {
            return new MassageJson(201,"删除失败",i);
        }

    }

    /**
     * 查询所有广告
     * @return 集合
     */
    @RequestMapping("/getALL")

    public MassageJson getALL(){
        List<Advertising> advertisingList = new ArrayList<Advertising>();
        try {
            advertisingList = advertisingService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(advertisingList.size()>0){
            return new MassageJson(200,"请求成功",advertisingList);
        }else {
            return new MassageJson(201,"请求失败",advertisingList);
        }

    }

    /**
     * 根据广告id从Redis获取浏览量 并让其进行+1操作
     * @param advertising_id
     * @return 返回广告对象
     * @throws Exception
     */

    @RequestMapping("/getTraffic")
    public MassageJson getTraffic(Long advertising_id){

        Long traffic = stringRedisTemplate.opsForValue().increment("advertising_id");

        return new MassageJson(200,"阅读量+1",traffic);
    }

    /**
     * 根据广告id更新浏览量
     * @param advertising_id
     * @param advertising_traffic
     * @return
     */

    @RequestMapping("/updTrafficById")
    public MassageJson updTrafficById(Long advertising_id,Long advertising_traffic){
        int i = 0;
        try {
            i = advertisingService.updTrafficById(advertising_id,advertising_traffic);
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(i>0){
            return new MassageJson(200,"更新阅读量成功",i);
        }else {
            return new MassageJson(201,"更新阅读量失败",i);
        }
    }

    /**
     * 777
     * 根据浏览量给广告排序
     * @return
     *//*
    @RequestMapping("/getAdvertisingByTraffic")
    @ResponseBody
    public MassageJson getAdvertisingByTraffic(){
        List<Advertising> advertisingList = null;
        try {
            advertisingList = advertisingService.getAdvertisingByTraffic();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(advertisingList!=null){
            return new MassageJson(200,"获取成功",advertisingList);
        }else {
            return new MassageJson(201,"获取失败",advertisingList);
        }

    }*/
}
